//
//  YFStatusPictureView.swift
//  YFSwiftWeibo
//
//  Created by tsaievan on 2017/9/8.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

class YFStatusPictureView: UIView {

   @IBOutlet weak var heightCons: NSLayoutConstraint!
    
    var viewModel: YFWeiboStatusViewModel? {
        didSet {
            setImageView()
        }
    }
    
    var urls: [YFWeiboPicModel]? {
        didSet {
            ///< 先将所有imageView都隐藏
            for iv in subviews {
                iv.isHidden = true
            }
            
            ///< 逐个设置每个iv的image
            var index = 0
            for url in urls ?? [] {
                let imageView = subviews[index] as! UIImageView
                ///< 这个地方要将imageView再次显示出来
                imageView.isHidden = false
                imageView.yf_setImage(with: url.thumbnail_pic, placeholderImage: nil)
                if index == 1 && urls?.count == 4 {
                    index += 1
                }
                index += 1
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
}

extension YFStatusPictureView {
    fileprivate func setupUI() {
        ///< 超出部分剪掉不显示
        clipsToBounds = true
        ///< 设置view的背景颜色跟父类相同
        backgroundColor = superview?.backgroundColor
        let count = 3
        let rect = CGRect(x: 0, y: YFWeiboPicViewOutterMargin, width: YFWeiboPicViewItemWidth, height: YFWeiboPicViewItemWidth)
        for i in 0 ..< count * count {
            let imageView = UIImageView()
            imageView.backgroundColor = UIColor.red
            ///< 设置imageView的填充模式
            imageView.contentMode = .scaleAspectFill
            ///< 超出部分需要剪掉
            imageView.clipsToBounds = true
            addSubview(imageView)
            ///< 计算行
            let row = i / count
            ///< 计算列
            let col = i % count
            ///< 计算偏移量
            let xOffset = (YFWeiboPicViewItemWidth + YFWeiboPicViewInnerMargin) * CGFloat(col) ///< x的偏移量是跟列有关的
            let yOffset = (YFWeiboPicViewItemWidth + YFWeiboPicViewInnerMargin) * CGFloat(row) ///< y的偏移量是跟行有关的
            imageView.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
        }
    }
    
    fileprivate func setImageView() {
        ///< 设置高度
        heightCons.constant = viewModel?.picViewSize.height ?? 0
        var imageViewSize = viewModel?.picViewSize ?? CGSize() 
        if viewModel?.status.pic_urls?.count == 1 { ///< 只有一张图
            
            ///< 对过宽图像的处理
            let maxWidth: CGFloat = 300
            if imageViewSize.width > maxWidth {
                imageViewSize.width = maxWidth
                imageViewSize.height = imageViewSize.height / maxWidth * imageViewSize.width
            }
            
            ///< 对过窄图像的处理
            let minWidth: CGFloat = 40
            if imageViewSize.width < minWidth {
                imageViewSize.width = minWidth
                ///< 除以4的原因是防止图片过于长而变成刷屏金箍棒
                imageViewSize.height = imageViewSize.height / minWidth * imageViewSize.width / 4
            }
            
            subviews[0].frame = CGRect(x: 0, y: YFWeiboPicViewOutterMargin, width: imageViewSize.width, height: imageViewSize.height - YFWeiboPicViewOutterMargin)
            
        }else {
            subviews[0].frame = CGRect(x: 0, y: YFWeiboPicViewOutterMargin, width: YFWeiboPicViewItemWidth, height: YFWeiboPicViewItemWidth)
        }
    }
}
